#!/bin/bash

# 数据库迁移脚本
ACTION=${1:-"update"}  # update, add, remove
MIGRATION_NAME=${2:-""}

PROJECT_PATH="src/TerritoryGame.Infrastructure"
STARTUP_PROJECT_PATH="src/TerritoryGame.API"

echo "🎯 涂色抢地盘游戏 - 数据库迁移工具"
echo "================================================"

case $ACTION in
    "add")
        if [ -z "$MIGRATION_NAME" ]; then
            echo "❌ 请提供迁移名称"
            echo "用法: ./migrate-database.sh add 'YourMigrationName'"
            exit 1
        fi
        
        echo "📝 创建新的数据库迁移: $MIGRATION_NAME"
        dotnet ef migrations add "$MIGRATION_NAME" -p "$PROJECT_PATH" -s "$STARTUP_PROJECT_PATH"
        ;;
    
    "update")
        echo "🔄 更新数据库到最新迁移"
        dotnet ef database update -p "$PROJECT_PATH" -s "$STARTUP_PROJECT_PATH"
        ;;
    
    "remove")
        echo "🗑️ 移除最后一个迁移"
        dotnet ef migrations remove -p "$PROJECT_PATH" -s "$STARTUP_PROJECT_PATH"
        ;;
    
    "list")
        echo "📋 列出所有迁移"
        dotnet ef migrations list -p "$PROJECT_PATH" -s "$STARTUP_PROJECT_PATH"
        ;;
    
    "seed")
        echo "🌱 运行种子数据"
        echo "启动应用程序以自动运行种子数据..."
        dotnet run --project "$STARTUP_PROJECT_PATH"
        ;;
    
    *)
        echo "❌ 未知的操作: $ACTION"
        echo "可用操作:"
        echo "  - add: 添加新迁移"
        echo "  - update: 更新数据库"
        echo "  - remove: 移除最后迁移"
        echo "  - list: 列出所有迁移"
        echo "  - seed: 运行种子数据"
        echo ""
        echo "示例:"
        echo "  ./migrate-database.sh update"
        echo "  ./migrate-database.sh add 'AddUserTable'"
        ;;
esac

echo "✅ 操作完成!"
